package it.unibz.easytravel.db;

import java.util.Date;
import java.util.Vector;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ExpensesDbAdapter {

	private static final String TAG = "ExpensesDbAdapter";

	private DatabaseHelper mDbHelper;

	private SQLiteDatabase mDb;

	private final Context mCtx;

	/**
	 * Database creation sql statement
	 */
	private static final String DATABASE_CREATE = "create table expenses ("
			+ "_id integer primary key autoincrement, " + "travel_id integer, "
			+ "amount integer, " + "comment text, " + "creditor integer, "
			+ "debtors text, " + "show boolean, " + "calculated boolean, "
			+ "date timestamp);";

	public static void onCreate(SQLiteDatabase db) {
		db.execSQL(DATABASE_CREATE);
	}

	public static void onUpgrade(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS expenses");
		onCreate(db);
	}

	public ExpensesDbAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	public ExpensesDbAdapter open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	/**
	 * method deletes all entries form table
	 */
	public void delete() {
		Log.i(TAG, "deleteTable");
		mDb.execSQL("DELETE FROM expenses");
	}

	public long createExpense(int travelId, int amount, int creditor,
			Vector<Integer> debtors, String comment, boolean show, boolean calculated,
			Date date) {

		Log.i(TAG, "createExpense");
		Log.w(TAG, "method not compleat!!");
		// gather the list of debtors
		// FIXME: for sure not the final version. Rewrite
		String debtors_tmp = "";
		int size = debtors.size();
		for (int i = 0; i < size; i++) {
			debtors_tmp += ((Integer) debtors.elementAt(i)).toString() + ",";
		}
		debtors_tmp = debtors_tmp.substring(0, debtors_tmp.length());

		ContentValues values = new ContentValues();
		values.put("travel_id", travelId);
		values.put("amount", amount);
		values.put("creditor", creditor);
		values.put("debtors", debtors_tmp);
		values.put("comment", comment);
		values.put("show", show);
		values.put("calculated", calculated);
		values.put("date", date.getTime());
		return mDb.insert("expenses", null, values);
	}

	/**
	 * Return a Cursor over the list of all notes in the database
	 * 
	 * @return Cursor over all notes
	 */
	public Cursor getAllExpenses() {
		Log.i(TAG, "getAllExpenses");
		return mDb.query("expenses", new String[] { "_id", "amount",
				"creditor", "debtors", "comment", "show" }, null, null, null,
				null, null);
	}
	
	/**
	 * Return a Cursor over the list of all notes in the database
	 * with belongs to travel with given travel_id
	 * 
	 * @param travelId
	 * @return Cursor over all notes
	 */
	public Cursor getExpenses(int travelId) {
		Log.i(TAG, "getAllExpenses(travelId)");
		String where = "travel_id=" + travelId;
		return mDb.query("expenses", new String[] { "_id", "amount",
				"creditor", "debtors", "comment", "show" }, where, null, null,
				null, null);
	}

	public Cursor getExpense(int travelId, int expenseId) {
		Log.i(TAG, "getAllExpenses");
		String where = "_id=" + expenseId + " and travel_id=" + travelId;
		return mDb.query("expenses", new String[] { "_id", "amount",
				"creditor", "debtors", "comment", "show" }, where, null, null,
				null, null);
	}

	/**
	 * UPDATE expenses SET show = false WHERE _id = expense_id AND traveler_id =
	 * travel_id
	 * 
	 * @param travel_id
	 * @param expense_id
	 */
	public void setInvisible(int travel_id, int expense_id) {
		Log.i(TAG, "setShowFalse");
		String where = "_id=" + expense_id + " and travel_id=" + travel_id;
		ContentValues values = new ContentValues();
		values.put("show", false);
		mDb.update("expenses", values, where, null);
	}

}
